package com.molichuxing.services.infrastructure.convertor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.molichuxing.services.business.dto.request.create.ContractBookingCarBizCreateDto;
import com.molichuxing.services.business.dto.request.create.ContractImageBizCreateDto;
import com.molichuxing.services.business.dto.request.create.ContractPurchaseBizCreateDto;
import com.molichuxing.services.business.dto.request.create.InvoiceBizCreateDto;
import com.molichuxing.services.business.dto.response.ContractImageBizDto;
import com.molichuxing.services.business.dto.response.ContractPurchaseBizDto;
import com.molichuxing.services.business.dto.response.InvoiceInfoBizDto;
import com.molichuxing.services.infrastructure.bean.*;
import com.molichuxing.services.infrastructure.dto.request.create.*;
import com.molichuxing.services.infrastructure.dto.request.modify.EsignAccountModifyDto;
import com.molichuxing.services.infrastructure.dto.request.modify.EsignCompanyModifyDto;
import com.molichuxing.services.infrastructure.dto.request.modify.EsignDealerModifyDto;
import com.molichuxing.services.infrastructure.dto.request.modify.InvoiceDto;
import com.molichuxing.services.infrastructure.dto.response.*;
import com.molichuxing.services.property.*;
import com.molichuxing.services.util.OSSUploadUtil;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.springframework.beans.BeanUtils.copyProperties;

/**
 * @author: zhaopenghui
 * @create_date: 2019年09月11日
 **/
public class Convert {


    /*********************************合同转换开始*************************************************/
    public static List<ContractDto> toContractDtoList(List<ContractBean> beanList) {
        List<ContractDto> dtoList = new ArrayList<>();
        if (beanList != null && beanList.size() > 0) {
            for (ContractBean contract : beanList) {
                dtoList.add(toContractDto(contract));
            }
        }
        return dtoList;
    }

    public static ContractDto toContractDto(ContractBean bean) {
        ContractDto beanDto = new ContractDto();
        if (bean != null) {
            copyProperties(bean, beanDto);
            beanDto.setContractId(bean.getId());
            Integer status = bean.getContractStatus();
            Integer type = bean.getContractType();
            beanDto.setContractPath(OSSUploadUtil.getUrlExpireTime(bean.getContractPath(), 3600 * 1000 * 24));
            if (null != status) {
                beanDto.setContractStatusStr(ContractStatusEnum.getName(status));
            }
            if (null != type) {
                beanDto.setContractTypeStr(ContractTypeEnum.getName(type));
            }
        }
        return beanDto;
    }


    public static List<ContractTemplateDto> toContractTemplateDto(List<ContractTemplateBean> beanList) {
        List<ContractTemplateDto> dtoList = new ArrayList<>();
        if (beanList != null && beanList.size() > 0) {
            for (ContractTemplateBean contract : beanList) {
                dtoList.add(toContractTemplateDto(contract));
            }
        }
        return dtoList;
    }

    public static ContractTemplateDto toContractTemplateDto(ContractTemplateBean bean) {
        ContractTemplateDto beanDto = new ContractTemplateDto();
        if (bean != null) {
            copyProperties(bean, beanDto);
            beanDto.setTemplateId(bean.getId());
        }
        return beanDto;
    }

    public static EsignAccountBean toEsignAccountBean(EsignAccountCreateDto esignAccountCreateDto) {
        EsignAccountBean esignAccountBean = new EsignAccountBean();
        if (esignAccountCreateDto != null) {
            copyProperties(esignAccountCreateDto, esignAccountBean);
        }
        return esignAccountBean;
    }

    public static EsignAccountBean toEsignAccountBean(EsignAccountModifyDto esignAccountModifyDto, EsignAccountBean esignAccountBean) {
        if (esignAccountModifyDto != null) {
            copyProperties(esignAccountModifyDto, esignAccountBean);
        }
        return esignAccountBean;
    }

    public static EsignCompanyBean toEsignCompanyBean(EsignCompanyCreateDto esignCompanyCreateDto) {
        EsignCompanyBean bean = new EsignCompanyBean();
        if (esignCompanyCreateDto != null) {
            copyProperties(esignCompanyCreateDto, bean);
        }
        return bean;
    }

    public static EsignCompanyBean toEsignCompanyBean(EsignCompanyModifyDto esignCompanyModifyDto, EsignCompanyBean esignCompanyBean) {
        if (esignCompanyModifyDto != null) {
            copyProperties(esignCompanyModifyDto, esignCompanyBean);
        }
        return esignCompanyBean;
    }

    public static EsignDealerBean toEsignDealerBean(EsignDealerCreateDto esignDealerCreateDto) {
        EsignDealerBean esignDealerBean = new EsignDealerBean();
        if (esignDealerCreateDto != null) {
            copyProperties(esignDealerCreateDto, esignDealerBean);
        }
        return esignDealerBean;
    }

    public static EsignDealerBean toEsignDealerBean(EsignDealerModifyDto esignDealerModifyDto, EsignDealerBean esignDealerBean) {
        if (esignDealerModifyDto != null) {
            copyProperties(esignDealerModifyDto, esignDealerBean);
        }
        return esignDealerBean;
    }

    public static ContractBean toContractBean(ContractCreateDto contractCreateDto) {
        ContractBean bean = new ContractBean();
        if (contractCreateDto != null) {
            copyProperties(contractCreateDto, bean);
        }
        return bean;
    }

    public static EsignAccountDto toEsignAccountDto(EsignAccountBean esignAccountBean) {
        EsignAccountDto dto = new EsignAccountDto();
        if (esignAccountBean != null) {
            copyProperties(esignAccountBean, dto);
        }
        return dto;
    }

    public static EsignCompanyDto toEsignCompanyDto(EsignCompanyBean esignCompanyBean) {
        EsignCompanyDto dto = new EsignCompanyDto();
        if (esignCompanyBean != null) {
            copyProperties(esignCompanyBean, dto);
            dto.setEsignCompanyId(esignCompanyBean.getId());
        }
        return dto;
    }

    public static EsignDealerDto toEsignDealerDto(EsignDealerBean esignDealerBean) {
        EsignDealerDto dto = null;
        if (esignDealerBean != null) {
            dto = new EsignDealerDto();
            copyProperties(esignDealerBean, dto);
            dto.setEsignDealerId(esignDealerBean.getId());
        }
        return dto;
    }

    public static List<EsignCompanyDto> toEsignCompanyDtoList(List<EsignCompanyBean> beanList) {
        List<EsignCompanyDto> dtoList = new ArrayList<>();
        if (beanList != null && beanList.size() > 0) {
            for (EsignCompanyBean bean : beanList) {
                dtoList.add(toEsignCompanyDto(bean));
            }
        }
        return dtoList;
    }

    public static List<ContractImageDto> toContractImageDtoList(List<ContractImageBean> beanList) {
        List<ContractImageDto> dtoList = new ArrayList<>();
        if (beanList != null && beanList.size() > 0) {
            for (ContractImageBean bean : beanList) {
                dtoList.add(toContractImageDto(bean));
            }
        }
        return dtoList;
    }

    public static ContractImageDto toContractImageDto(ContractImageBean bean) {
        ContractImageDto beanDto = new ContractImageDto();
        if (bean != null) {
            copyProperties(bean, beanDto);
        }
        return beanDto;
    }

    public static ContractCreateDto toContractCreateDto(ContractPurchaseBizCreateDto bizDto) {
        ContractCreateDto beanDto = new ContractCreateDto();
        if (bizDto != null) {
            copyProperties(bizDto, beanDto);
        }
        return beanDto;
    }

    public static ContractPurchaseBizDto toContractPurchaseBizDto(ContractDto dto) {
        ContractPurchaseBizDto bizDto = new ContractPurchaseBizDto();
        if (dto != null) {
            copyProperties(dto, bizDto);
        }
        return bizDto;
    }

    public static List<ContractImageBizDto> toContractImageBizDtoList(List<ContractImageDto> dtoList) {
        List<ContractImageBizDto> bizDtoList = new ArrayList<>();
        if (dtoList != null && dtoList.size() > 0) {
            for (ContractImageDto contractImageDto : dtoList) {
                bizDtoList.add(toContractImageBizDto(contractImageDto));
            }
        }
        return bizDtoList;
    }

    public static ContractImageBizDto toContractImageBizDto(ContractImageDto dto) {
        ContractImageBizDto beanDto = new ContractImageBizDto();
        if (dto != null) {
            copyProperties(dto, beanDto);
        }
        return beanDto;
    }


    public static List<EsignDealerDto> toEsignDealerDtoList(List<EsignDealerBean> beanList) {
        List<EsignDealerDto> dtoList = null;
        if (beanList != null && beanList.size() > 0) {
            dtoList = new ArrayList<>();
            for (EsignDealerBean bean : beanList) {
                dtoList.add(toEsignDealerDto(bean));
            }
        }
        return dtoList;
    }

    public static ContractCreateDto toContractCreateDto(ContractBookingCarBizCreateDto bizDto) {
        ContractCreateDto beanDto = new ContractCreateDto();
        if (bizDto != null) {
            copyProperties(bizDto, beanDto);
        }
        return beanDto;
    }

    public static ContractEnterpriseBean toContractEnterpriseBean(ContractEnterpriseCreateDto createDto) {
        ContractEnterpriseBean bean = null;
        if (createDto != null) {
            bean = new ContractEnterpriseBean();
            copyProperties(createDto, bean);
            bean.setContractUrl(JSON.toJSONString(createDto.getContractUrls()));
            if (createDto.getContractDate() == null) {
                bean.setContractDate(LocalDate.now());
            }
        }
        return bean;
    }

    public static List<ContractEnterpriseDto> toContractEnterpriseDtoList(List<ContractEnterpriseBean> beanList) {
        List<ContractEnterpriseDto> dtoList = new ArrayList<>();
        if (beanList != null && beanList.size() > 0) {
            for (ContractEnterpriseBean contract : beanList) {
                dtoList.add(toContractEnterpriseDto(contract));
            }
        }
        return dtoList;
    }

    public static ContractEnterpriseDto toContractEnterpriseDto(ContractEnterpriseBean bean) {
        ContractEnterpriseDto beanDto = null;
        if (bean != null) {
            beanDto = new ContractEnterpriseDto();
            copyProperties(bean, beanDto);
            beanDto.setContractId(bean.getId());
            Integer type = bean.getContractType();
            if (null != type) {
                beanDto.setContractTypeStr(ContractEnterpriseTypeEnum.getName(type));
            }
            beanDto.setContractUrls(bean.getContractUrl() == null
                    ? null : JSONArray.parseArray(bean.getContractUrl(), ContractFileDto.class));
        }
        return beanDto;
    }

    /*********************************合同转换结束*************************************************/

    /*********************************发票转换开始*************************************************/
    public static List<InvoiceApplyDto> toInvoiceApplyDtoList(List<InvoiceApplyBean> beanList) {
        List<InvoiceApplyDto> dtoList = new ArrayList<>();
        if (beanList != null && beanList.size() > 0) {
            for (InvoiceApplyBean bean : beanList) {
                dtoList.add(toInvoiceApplyDto(bean));
            }
        }
        return dtoList;
    }

    public static InvoiceApplyDto toInvoiceApplyDto(InvoiceApplyBean bean) {
        InvoiceApplyDto beanDto = new InvoiceApplyDto();
        if (bean != null) {
            copyProperties(bean, beanDto);
            beanDto.setInvoiceApplyId(bean.getId());
            beanDto.setInvoiceTitleTypeStr(InvoiceTitleTypeEnum.getName(bean.getInvoiceTitleType()));
        }
        return beanDto;
    }

    public static List<InvoiceInfoDto> toInvoiceInfoDtoList(List<InvoiceInfoBean> beanList) {
        List<InvoiceInfoDto> dtoList = new ArrayList<>();
        if (beanList != null && beanList.size() > 0) {
            for (InvoiceInfoBean bean : beanList) {
                dtoList.add(toInvoiceInfoDto(bean));
            }
        }
        return dtoList;
    }

    public static InvoiceInfoDto toInvoiceInfoDto(InvoiceInfoBean bean) {
        InvoiceInfoDto beanDto = new InvoiceInfoDto();
        if (bean != null) {
            copyProperties(bean, beanDto);
            beanDto.setInvoiceInfoId(bean.getId());

            Integer businessType = bean.getBusinessType();
            if (businessType != null) {
                beanDto.setBusinessTypeStr(InvoiceBusinessTypeEnum.getName(businessType));
            }
        }
        return beanDto;
    }

    public static List<InvoiceInfoBizDto> toInvoiceInfoBizDtoList(List<InvoiceInfoDto> dtoList) {
        List<InvoiceInfoBizDto> bizDtoList = new ArrayList<>();
        if (dtoList != null && dtoList.size() > 0) {
            for (InvoiceInfoDto dto : dtoList) {
                bizDtoList.add(toInvoiceInfoBizDto(dto));
            }
        }
        return bizDtoList;
    }

    public static InvoiceInfoBizDto toInvoiceInfoBizDto(InvoiceInfoDto dto) {
        InvoiceInfoBizDto bizDto = new InvoiceInfoBizDto();
        if (dto != null) {
            copyProperties(dto, bizDto);
        }
        return bizDto;
    }

    public static InvoiceInfoBizDto toInvoiceInfoBizDto(InvoiceApplyDto invoiceApplyDto, InvoiceInfoBizDto invoiceInfoBizDto) {
        if (invoiceApplyDto != null) {
            Integer status = invoiceInfoBizDto.getStatus();
            String remark = invoiceInfoBizDto.getRemark();

            copyProperties(invoiceApplyDto, invoiceInfoBizDto);
            invoiceInfoBizDto.setStatus(status);
            invoiceInfoBizDto.setRemark(remark);
        }
        return invoiceInfoBizDto;
    }

    public static InvoiceInfoBizDto toInvoiceInfoBizDto(InvoiceDetailDto invoiceDetailDto, InvoiceInfoBizDto invoiceInfoBizDto) {
        if (invoiceDetailDto != null) {
            String imageFiles = invoiceDetailDto.getImageFiles();
            invoiceInfoBizDto.setImageFiles(imageFiles);
        }
        return invoiceInfoBizDto;
    }

    public static InvoiceApplyBean toInvoiceInfoBizDto(InvoiceApplyCreateDto invoiceApplyCreateDto) {
        InvoiceApplyBean invoiceApplyBean = new InvoiceApplyBean();
        if (invoiceApplyCreateDto != null) {
            copyProperties(invoiceApplyCreateDto, invoiceApplyBean);
        }
        return invoiceApplyBean;
    }

    public static InvoiceApplyCreateDto toInvoiceApplyCreateDto(InvoiceBizCreateDto invoiceBizCreateDto, InvoiceDto invoiceDto) {
        InvoiceApplyCreateDto invoiceApplyCreateDto = new InvoiceApplyCreateDto();
        if (invoiceBizCreateDto != null) {
            copyProperties(invoiceBizCreateDto, invoiceApplyCreateDto);
            invoiceApplyCreateDto.setStatus(InvoiceApplyStatusEnum.MAKE_OUT.getValue());
            invoiceApplyCreateDto.setOrderCode(invoiceDto.getOrderCode());
            invoiceApplyCreateDto.setInvoiceApplyAmount(invoiceDto.getAmount());
            invoiceApplyCreateDto.setInvoiceType(InvoiceApplyTypeEnum.E_INVOICE.getValue());
            invoiceApplyCreateDto.setBusinessType(invoiceDto.getBusinessType());
        }
        return invoiceApplyCreateDto;
    }


    public static InvoiceBean toInvoiceBean(InvoiceCreateDto dto) {
        InvoiceBean bean = new InvoiceBean();
        if (dto != null) {
            copyProperties(dto, bean);
        }
        return bean;
    }

    public static List<InvoiceDto> toInvoiceDtoList(List<InvoiceBean> beanList) {
        List<InvoiceDto> dtoList = new ArrayList<>();
        if (beanList != null && beanList.size() > 0) {
            for (InvoiceBean bean : beanList) {
                dtoList.add(toInvoiceDto(bean));
            }
        }
        return dtoList;
    }

    public static InvoiceDto toInvoiceDto(InvoiceBean bean) {
        InvoiceDto beanDto = new InvoiceDto();
        if (bean != null) {
            copyProperties(bean, beanDto);
            beanDto.setInvoiceId(bean.getId());
            Integer businessType = bean.getBusinessType();
            Integer type = bean.getType();

            if (businessType != null) {
                beanDto.setBusinessTypeStr(InvoiceBusinessTypeEnum.getName(businessType));
            }
            if (type != null) {
                beanDto.setTypeStr(InvoiceTypeEnum.getName(type));
            }
        }
        return beanDto;
    }

    public static Map<Integer, InvoiceDto> toInvoiceDtoMap(List<InvoiceBean> beanList) {
        Map<Integer, InvoiceDto> dtoMap = new HashMap<>();
        if (beanList != null && beanList.size() > 0) {
            for (InvoiceBean bean : beanList) {
                if (bean != null) {
                    Integer id = bean.getId();
                    dtoMap.put(id, toInvoiceDto(bean));
                }
            }
        }
        return dtoMap;
    }


    public static List<ContractImageCreateDto> toContractImageCreateDtoList(List<ContractImageBizCreateDto> bizList, Integer contractId) {
        List<ContractImageCreateDto> dtoList = new ArrayList<>();
        if (bizList != null && bizList.size() > 0) {
            for (ContractImageBizCreateDto bizDto : bizList) {
                dtoList.add(toContractImageCreateDto(bizDto, contractId));
            }
        }
        return dtoList;
    }

    public static ContractImageCreateDto toContractImageCreateDto(ContractImageBizCreateDto bizDto, Integer contractId) {
        ContractImageCreateDto beanDto = new ContractImageCreateDto();
        if (bizDto != null) {
            copyProperties(bizDto, beanDto);
            beanDto.setContractId(contractId);
        }
        return beanDto;
    }

    public static List<InvoiceDetailDto> toInvoiceDetailDtoList(List<InvoiceDetailBean> beanList) {
        List<InvoiceDetailDto> dtoList = new ArrayList<>();
        if (beanList != null && beanList.size() > 0) {
            for (InvoiceDetailBean bean : beanList) {
                dtoList.add(toInvoiceDetailDto(bean));
            }
        }
        return dtoList;
    }

    public static InvoiceDetailDto toInvoiceDetailDto(InvoiceDetailBean bean) {
        InvoiceDetailDto beanDto = null;
        if (bean != null) {
            beanDto = new InvoiceDetailDto();
            copyProperties(bean, beanDto);
            beanDto.setInvoiceDetailId(bean.getId());
            beanDto.setImageFiles(OSSUploadUtil.getUrlExpireTime(bean.getImageFiles(), 3600 * 1000 * 24));
        }
        return beanDto;
    }

    /*********************************发票转换结束*************************************************/

    public static List<ContractLogDto> toContractLogDto(List<ContractLogBean> beanList) {
        List<ContractLogDto> dtoList = null;
        ;
        if (beanList != null && beanList.size() > 0) {
            dtoList = new ArrayList<>();
            for (ContractLogBean bean : beanList) {
                dtoList.add(toContractLogDto(bean));
            }
        }

        return dtoList;
    }

    public static ContractLogDto toContractLogDto(ContractLogBean bean) {
        ContractLogDto beanDto = null;
        if (bean != null) {
            beanDto = new ContractLogDto();
            copyProperties(bean, beanDto);
        }
        return beanDto;
    }

    public static ContractLogBean toContractLogBean(ContractLogDto dto) {
        ContractLogBean bean = null;
        if (dto != null) {
            bean = new ContractLogBean();
            copyProperties(dto, bean);
        }
        return bean;

    }

    public static List<ContractLogBean> toContractLogBean(List<ContractLogDto> dtoList) {
        List<ContractLogBean> beanList = null;
        if (dtoList != null && dtoList.size() > 0) {
            beanList = new ArrayList<>();
            for (ContractLogDto bean : dtoList) {
                beanList.add(toContractLogBean(bean));
            }
        }
        return beanList;

    }
}
